package com.spyx.product.receive;


import com.rabbitmq.client.Channel;
import com.spyx.common.constant.MqConst;
import com.spyx.product.service.SkuInfoService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//接收扣减库存的mq消息
@Component
public class StockReceiver {

    @Autowired
    private SkuInfoService skuInfoService;

    @RabbitListener(
            bindings = @QueueBinding(value =@Queue(value = MqConst.QUEUE_MINUS_STOCK,durable = "true"),
                    exchange = @Exchange(value = MqConst.EXCHANGE_ORDER_DIRECT),
                    key = {MqConst.ROUTING_MINUS_STOCK})
    )
    public void changeStock(String orderNo, Message message, Channel channel) throws  Exception{
        System.out.println("接收消息-订单编号-"+orderNo);
        //1.获取消息中的orderno
        if(orderNo!=null){
            //调取扣减库存的方法
            skuInfoService.deductStock(orderNo);
        }
        //2.消息手动确认
        /**
         * 第一个参数：表示收到的消息的标号
         * 第二个参数：如果为true表示可以签收多个消息
         */
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
